package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInPapersAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInPapersBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationInPaperRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.ArrayList;
import java.util.List;

/**
 * 调拨入库
 *
 * @author 詹建宇
 * @time on 2018/4/3 15:06
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInPaperActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;

    private String mCondition;//关键字

    private List<AllocationInPapersBean.BodyBean> allData;

    private AllocationInPapersAdapter adapter;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_listview);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);
        listView.setPullDownFlag(false);
        listView.setPullUpFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("调拨入库");
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_receive_list_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        allData = new ArrayList<>();
        mCondition = "";
        addListData(activity, allData, listView, true);
    }

    private void addListData(Activity context, List<AllocationInPapersBean.BodyBean> list, ListView listView, boolean flag) {
        adapter = new AllocationInPapersAdapter(context, list, mCondition, new SearchListener() {
            @Override
            public NoDoubleClickListener getNoDoubleClickListener(EditText condition) {
                NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        Utils.closeSoftKeyboard(context, v);
                        mCondition = condition.getText().toString();
                        search(context, mCondition);
                    }
                };
                return noDoubleClickListener;
            }
        });
        listView.setAdapter(adapter);
    }

    private void search(Activity context, String condition) {
        AllocationInPaperRequest.request(context, condition, new GsonBeanListener<AllocationInPapersBean>() {
            @Override
            public void onSuccess(AllocationInPapersBean bean) {
                allData = bean.getBody();
                addListData(context, allData, listView, true);
            }
        });
    }

    public interface SearchListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText condition);
    }
}
